﻿using ASmile.DBModel;
using ASmile.Extend;
using ASmile.ORM.Cores;

namespace ASmile.Com.Da
{
    public class RoleUrlBLL : BLLCrudBase<ArgMainPageData, Sys_RoleUrl>, IBLLCrud<ArgMainPageData, Sys_RoleUrl>
    {
        protected override void SetQuery(QueryCondition<Sys_RoleUrl> query, ArgMainPageData arg)
        {
            query.WhereAnd(s => s.RoleId == arg.MainId);
            if (arg.SearchVal.IsEmpty())
            {
                string likeStr = arg.SearchValLike;
                query.WhereAnd(s =>
                    s.WEx_Like(s.MatchValue, likeStr) ||
                    s.WEx_Like(s.IPStrs, likeStr)
                );
            }
        }

        protected override ApiResult<string> VerifyModel(Sys_RoleUrl model, bool isAdd)
        {
            if (model.RoleId.IsEmpty())
            {
                return RetErr("RoleId", "角色不能为空");
            }
            if (model.MatchMode == 0)
            {
                return RetErr("MatchMode", "匹配模式错误");
            }
            if (model.MatchValue.IsEmpty())
            {
                return RetErr("MatchValue", "匹配值不能为空");
            }
            if (model.IsLimitIP && model.IPStrs.IsEmpty())
            {
                return RetErr("IPStrs", "IP匹配值不能为空");
            }
            if (isAdd)
            {
                model.Id = NewId();
                return RetOK(model.Id, "新增成功");
            }
            return RetOK("", "更新成功");
        }
    }
}